﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace HotelManagementClient.Utlis
{
    public class NetWorkUtil
    {
        
        [DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
        public extern static bool InternetGetConnectedState(out int conState, int reader);

        public static bool IsNetworkConnected
        {
            get
            {
                return InternetGetConnectedState(out int n, 0);
            }
        }

        public static bool CheckUrlVisit(string url)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                if(resp.StatusCode == HttpStatusCode.OK)
                {
                    return true;
                }
            }
            catch(WebException e)
            {
                return false;
            }
            return false;
        }



        public static bool CheckWebConnection(string Url)
        {
            bool result = false;
            //测试服务连接
            HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
            try
            {
                request.Method = "GET";
                request.ProtocolVersion = new Version(1, 1);
                request.Timeout = 2000;


                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
                response.Dispose();
            }
            catch (Exception e)
            {
                result = false;
            }

            return result;
        }

    }
}
